package com.lesso.yjy.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yjy.sunflower.domain.repository.AttendanceRuleRepository;
import com.lesso.yjy.sunflower.infra.assembler.AttendanceRuleAssembler;
import com.lesso.yjy.sunflower.infra.mapper.AttendanceRuleMapper;
import com.lesso.yjy.sunflower.model.AttendanceRule;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.yjy.sunflower.pojo.vo.AttendanceRuleVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class AttendanceRuleRepositoryImpl implements AttendanceRuleRepository {
    private final AttendanceRuleMapper ruleMapper;

    public AttendanceRuleRepositoryImpl(AttendanceRuleMapper ruleMapper){this.ruleMapper = ruleMapper;}

    @Override
    public Long save(AttendanceRule rule) {
        rule.setDelStatus(0);
        ruleMapper.insert(rule);
        return rule.getId();
    }

    @Override
    public Page<AttendanceRuleVO> getPageList(AttendanceRuleQueryDTO dto) {
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<AttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getRuleName()), AttendanceRule.RULE_NAME, dto.getRuleName());
        List<AttendanceRule> rules = ruleMapper.selectPage(page, wrapper);
        return page.setRecords(AttendanceRuleAssembler.INSTANCE.entityListToVOList(rules));
    }

    @Override
    public Page<AttendanceRuleVO> getCustomPageList(AttendanceRuleQueryDTO dto){
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(),dto.getSize());
        List<AttendanceRuleVO> rules= ruleMapper.selectPageList(page, dto);
        return page.setRecords(rules);
    }

    @Override
    public void removeById(Long id){
        ruleMapper.deleteById(id);
    }

    @Override
    public void updateById(AttendanceRuleDTO rule){
        AttendanceRule entity = AttendanceRuleAssembler.INSTANCE.dtoToEntity(rule);
        ruleMapper.updateById(entity);
    }
}
